package com.bing.my.controller;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bing.common.config.Result;
import com.bing.my.entity.Member;
import com.bing.my.entity.Recharge;
import com.bing.my.entity.vo.AddMoneyForm;
import com.bing.my.service.RechargeService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 充值数据 前端控制器
 * </p>
 *
 * @author bing
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/recharge")
public class RechargeController {
    @Autowired
    private RechargeService rechargeService;

    /**
    * 充值业务
     * 需要登录授权
    * @author Bing
    * @date 2021/10/20 16:25
     * @param addMoneyForm
    * @return com.bing.common.config.Result<?>
    */
    @RequiresAuthentication
    @PostMapping
    public Result<?> insert(@Validated @RequestBody AddMoneyForm addMoneyForm) {
        Recharge recharge = addMoneyForm.getRecharge();
        String id = addMoneyForm.getId();
        if (rechargeService.addMoney(recharge,id)) {
            return Result.success();
        }
        return Result.error(503,"充值失败!");
    }
    /**
    * 分页查询充值明细
    * @author Bing
    * @date 2021/10/20 19:57
     * @param pageNum
     * @param pageSize
     * @param memberId
    * @return com.bing.common.config.Result<?>
    */
    @RequiresAuthentication
    @GetMapping
    public Result<?> findPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String memberId) {
        Page<Recharge> page = rechargeService.selectPage(pageNum, pageSize, memberId);
        return Result.success(page);
    }
    @RequiresAuthentication
    @PutMapping
    public Result<?> update(@Validated @RequestBody Recharge recharge) {
        int update = rechargeService.update(recharge);
        if (update != 1) {
            return Result.error(501, "修改失败!");
        }
        return Result.success("修改成功!");
    }
    @RequiresAuthentication
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable String id) {
        int deleteById = rechargeService.deleteById(id);
        if (deleteById != 1) {
            return Result.error(501, "删除失败!");
        }
        return Result.success("删除成功!");
    }

}

